/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * Nihongo
 * Copyright (C) Massimiliano Maniscalco 2011, 2012 <massi.neptune@yahoo.com>
 * 
Nihongo is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Nihongo is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "printable_window.h"
#include "kakikata_sheet.h"
#include "oboeru_sheet.h"
#include "print_kanji_table_operation.h"

PrintableWindow::
PrintableWindow () :
	m_vBox ( Gtk::ORIENTATION_VERTICAL, 0 ), _scrolledWin ( )
{
	set_default_size ( 600, 500 );
	set_title ( "Stampa queste pagine per esercitarti" );
	set_modal ( true );
	add ( m_vBox );
	m_vBox.pack_end ( _scrolledWin, true, true, 0 );
	build_menu ();
	show_all_children ();
}

PrintableWindow::
~PrintableWindow ()
{
	delete _kakuArea;
}

void PrintableWindow::
build_menu ()
{
	//Create actions for menus and toolbars:
	m_refActionGroup = Gtk::ActionGroup::create();
	//File menu:
	m_refActionGroup->add( Gtk::Action::create( "FileMenu", "_File" ) );
	m_refActionGroup->add( Gtk::Action::create( "Print", Gtk::Stock::PRINT ), sigc::mem_fun( *this, &PrintableWindow::onMenuPrint ) );
	m_refActionGroup->add( Gtk::Action::create( "Close", Gtk::Stock::CLOSE ), sigc::mem_fun( *this, &PrintableWindow::onMenuClose ) );

	m_refUIManager = Gtk::UIManager::create();
	m_refUIManager->insert_action_group(m_refActionGroup);

	add_accel_group(m_refUIManager->get_accel_group());

    //Layout the actions in a menubar and toolbar:

    Glib::ustring ui_info =
        "<ui>"
        "  <menubar name='MenuBar'>"
        "    <menu action='FileMenu'>"
        "      <menuitem action='Print'/>"
        "      <separator/>"
        "      <menuitem action='Close'/>"
        "    </menu>"
        "  </menubar>"
        "</ui>";

	try
	{
		m_refUIManager->add_ui_from_string(ui_info);
	}
	catch(const Glib::Error& ex)
	{
		std::cerr << "building menus failed: " << ex.what();
	}

	//Get the menubar and toolbar widgets, and add them to a container widget:
	Gtk::Widget* pMenubar = m_refUIManager->get_widget("/MenuBar");
	if(pMenubar)
		m_vBox.pack_start(*pMenubar, Gtk::PACK_SHRINK);
}

void PrintableWindow::
on_printOperation_status_changed ( const Glib::RefPtr<PrintKanjiTableOperation>& operation )
{
	Glib::ustring status_msg;
	if ( operation->is_finished () )
		status_msg = "Print job completed.";
	else
		status_msg = operation->get_status_string ();
	cout << status_msg.raw() << endl;
}

void PrintableWindow::
on_printOperation_done ( Gtk::PrintOperationResult result, const Glib::RefPtr<PrintKanjiTableOperation>& operation )
{
	if ( result == Gtk::PRINT_OPERATION_RESULT_ERROR )
	{
		Gtk::MessageDialog err_dialog ( *this, "Error printing kanji table", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true );
		err_dialog.run();
	}
	else if ( result == Gtk::PRINT_OPERATION_RESULT_APPLY )
		m_refSettings = operation->get_print_settings ();
	if ( !operation->is_finished () )
		operation->signal_status_changed ().connect (sigc::bind (sigc::mem_fun ( *this, &PrintableWindow::on_printOperation_status_changed ), operation ) );
}

void PrintableWindow::
print_or_preview ( Gtk::PrintOperationAction print_action )
{
	Glib::RefPtr<PrintKanjiTableOperation> print = PrintKanjiTableOperation::create ();
	print->set_kaku_area ( _kakuArea );
	print->set_track_print_status ();
	print->set_default_page_setup ( m_refPageSetup );
	print->set_print_settings ( m_refSettings );
	print->signal_done ().connect (sigc::bind (sigc::mem_fun (*this, &PrintableWindow::on_printOperation_done ), print ) );
	try
	{
		print->run ( print_action, *this );
	}
	catch ( const Gtk::PrintError& ex )
	{
		cerr << "An error occurred while trying to run a print operation:" << ex.what() << endl;
	}
}

void PrintableWindow::
onMenuPrint ()
{
	print_or_preview(Gtk::PRINT_OPERATION_ACTION_PRINT_DIALOG);
}

void PrintableWindow::
onMenuClose ()
{
	hide();
}
